<template>
  <div class="flex flex-col h-full">
    <SearchContainer show-export @search="getData" @reset="() => formRef.resetFields()" @export="exportRef.$el.querySelector('button').click()">
      <a-form ref="formRef" class="form-style-flex" :model="formState" :label-col="{ style: { width: '80px' } }">
        <a-form-item name="dateRange">
          <a-range-picker v-model:value="formState.dateRange" format="YYYY-MM" picker="month" value-format="YYYY-MM" />
        </a-form-item>
      </a-form>
    </SearchContainer>
    <a-card class="flex-1">
      <a-space ref="exportRef" class="hidden">
        <BaseExport type="报表" name="月度手术按医生统计" code="MAP000443" is-show-time :search-params="searchParams">导出</BaseExport>
      </a-space>
      <BaseTable
        v-if="columns.length && !loading"
        row-key="序号"
        :scroll="{ x: columns.length * 130 }"
        :loading="loading"
        :data-source="tableData"
        fill
        :columns="columns"
      >
        <!-- <template #bodyCell="{ column, index }">
          <template v-if="column.dataIndex === '序号'">
            {{ index + 1 }}
          </template>
        </template> -->
      </BaseTable>
    </a-card>
  </div>
</template>

<script setup>
import { onMounted, ref } from 'vue'
import dayjs from 'dayjs'
import { getSystemConfig } from '@/utils/systemConfig'
import SearchContainer from '@/components/SearchContainer'
import BaseTable from '@/components/BaseTable'

const exportRef = ref(null)
const formRef = ref(null)
const day = ref('01')
const formState = ref({
  dateRange: [dayjs().year() + '-01', dayjs().subtract(1, 'month').format('YYYY-MM')]
})
const columns = ref([])
const { loading, tableData, getData } = useTable({
  url: '/statistic/getMonthlySurgeriesCountedByDoctor',
  params: formState.value,
  formatParams: () => {
    day.value = getSystemConfig('FINACE_STATISTICS_START_DATE')
    if (day.value <= 9) {
      day.value = '-0' + Number(day.value)
    } else {
      day.value = '-' + Number(day.value)
    }
    return {
      stdate: formState.value.dateRange?.[0] + day.value,
      eddate: formState.value.dateRange?.[1] + day.value
    }
  },
  pagination: false,
  immediate: true,
  onSuccess: (res) => {
    if (res && res.length > 0) {
      const idx = findObjectWithMostKeyValuePairs(res)
      columns.value = Object.keys(res[idx]).map((item) => {
        const align = item === '序号' || item === '统计月份' ? 'left' : 'right'
        const width = item === '序号' ? 60 : ''
        const customRender = item.indexOf('金额') > -1 || item.indexOf('费') > -1 ? ({ text }) => formatAmount(text, 2) : null
        return { title: item, dataIndex: item, customRender, align, ellipsis: true, width }
      })
    }
  }
})
const searchParams = ref({})
const formatTime = () => {
  day.value = getSystemConfig('FINACE_STATISTICS_START_DATE')
  if (day.value <= 9) {
    day.value = '-0' + Number(day.value)
  } else {
    day.value = '-' + Number(day.value)
  }
  return {
    stdate: formState.value.dateRange[0] + day.value,
    eddate: formState.value.dateRange[1] + day.value
  }
}
onMounted(() => {
  searchParams.value = formatTime()
})
const findObjectWithMostKeyValuePairs = (arr) => {
  const arrObj = []
  arr.forEach((item, index) => {
    arrObj.push({ num: Object.keys(item).length, idx: index })
  })
  const max = arrObj.sort((a, b) => b.num - a.num)[0].idx // 40
  return max // 返回键值对最多的对象
}
</script>
